<% include ssl_cert_class_js %>
<script>
var host = undefined;
var port = undefined;
$(document).ready(function(){
    $("#ssl-result").hide();
    $("#start").click(function(){

	  host = $("input[id='host']").val();
		port = $("input[id='port']").val();
		if (host.length === 0){
			alert("请输入扫描主机名称或IP");
      return;
    }

    $.ajax({
      url: "/api/ssl",
      data: {
        host: host,
        port: port
        },
      type: "POST"
    })

    .done(function( json ) {
      setTimeout(function () { pullExec() } , 1000);
      $("#ssl-result").hide();
      $("#loading").modal('show');
    })
    // Code to run if the request fails; the raw request and
    // status codes are passed to the function
    .fail(function( xhr, status, errorThrown ) {
      alert( `扫描出现错误(${status})`);
      console.log( "Error: " + errorThrown );
      console.log( "Status: " + status );
      console.dir( xhr );
    })

	});
});


function pullExec() {
  $.ajax({
    url: "/api/ssl",
    data: {
      },
    type: "GET"
  })

  .done(function( result ) {
    console.dir(result);
    var cert = processResult(result);
    displayResult(cert);
		$("#loading").modal('hide');
    $("#ssl-result").show();
  })
  // Code to run if the request fails; the raw request and
  // status codes are passed to the function
  .fail(function( xhr, status, errorThrown ) {
    if (status == '100') {
      setTimeout(function (){ pullExec() } , 1000);
      return;
    }
    alert( `扫描出现错误(${errorThrown})` );
    console.log( "Error: " + errorThrown );
    console.log( "Status: " + status );
    console.dir( xhr );
    $("#loading").modal('hide');
  })
}

/* 检查证书的4个标志：是否权威、是否在有效期内、身份是否相符、加密强度*/
function processResult(result) {
  return new Certificate(result.cert, host);
}

function displayResult(cert) {
  //总揽
  var t = cert.getSummary();
  var s = cert.getSubject();
  var i = cert.getIssuer();


  $( "#home span" ).removeClass("glyphicon-question-sign glyphicon-remove glyphicon-ok");
  /*
  $( "#home .noexpire > span" ).removeClass("glyphicon-ok glyphicon-remove");
  $( "#home .hostname > span" ).removeClass("glyphicon-ok glyphicon-remove");
  $( "#home .cipher > span" ).removeClass("glyphicon-ok glyphicon-remove");
  */
  $( "#authority" ).addClass(t.authority?"glyphicon-ok":"glyphicon-remove");
  $( "#noexpire" ).addClass(t.noexpire?"glyphicon-ok":"glyphicon-remove");
  $( "#hostname" ).addClass(t.hostname?"glyphicon-ok":"glyphicon-remove");
  $( "#cipher" ).addClass(t.cipher?"glyphicon-ok":"glyphicon-remove");

  $( "#subject .country" ).text(s.C)
  $( "#subject .state" ).text(s.ST)
  $( "#subject .local" ).text(s.L)
  $( "#subject .ou" ).text(s.OU)
  $( "#subject .o" ).text(s.O)
  $( "#subject .cn" ).text(s.CN)

  $( "#issuer .country" ).text(i.C)
  $( "#issuer .ou" ).text(i.OU)
  $( "#issuer .o" ).text(i.O)
  $( "#issuer .cn" ).text(i.CN)

  var misc = cert.getMiscInfo();
  $( "#misc .from" ).text(misc.valid_from)
  $( "#misc .to" ).text(misc.valid_to)
  $( "#misc .cipher" ).text(misc.cipher)
  $( "#misc .pub-key-len" ).text(misc.pub_key_len)
  $( "#misc .pub-key-val" ).text(misc.pub_key_val)

  var chains = cert.getCertChain();
  var chains_element = $( "#cert-chain" );
  chains_element.empty();
  for (var i=0; i<chains.length; i++) {
    var level = "level"+(i+1);
    var item = $('<li/>').addClass(`list-group-item ${level}`);
    item.append($('<i/>').
      attr('aria-hidden', 'true').
      addClass('fa fa-certificate'));
    item.append(chains[i]);
    chains_element.append(item);
  }
}
</script>
